package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.EhmResidentCrowdMapper;
import com.aijk.ehm.model.EhmResidentCrowd;
import com.aijk.ehm.model.EhmResidentCrowdExample;
import com.aijk.ehm.model.qo.EhmResidentCrowdQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EhmResidentCrowdService {
    private static Logger logger = LoggerFactory.getLogger(EhmResidentCrowdService.class);

    @Autowired
    private EhmResidentCrowdMapper mapper;

    public EhmResidentCrowdExample getExample(EhmResidentCrowdQo qo) {
        EhmResidentCrowdExample example = new EhmResidentCrowdExample();
        EhmResidentCrowdExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (Utils.isNotEmpty(qo.getUid())) criteria.andUidEqualTo(qo.getUid());
            if (Utils.isNotEmpty(qo.getResidentId())) criteria.andResidentIdEqualTo(qo.getResidentId());
        }
        return example;
    }

    public int count(EhmResidentCrowdQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(EhmResidentCrowd bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }
    public int deleteQo(EhmResidentCrowdQo qo) {
        return mapper.deleteByExample(getExample(qo));
    }

    public int update(EhmResidentCrowd bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public EhmResidentCrowd findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<EhmResidentCrowd> find(EhmResidentCrowdQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<EhmResidentCrowd> page(PagingRequest pagingRequest, EhmResidentCrowdQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<EhmResidentCrowd>(mapper.selectByExample(getExample(qo)));
    }
}